package com.JZ.QM.service.impl;

import com.JZ.QM.common.dao.ComEarningRecordDao;
import com.JZ.QM.common.dao.PositionDetailDao;
import com.JZ.QM.common.dao.WealthAccountDao;
import com.JZ.QM.common.entity.ComEarningRecordEntity;
import com.JZ.QM.common.entity.PositionDetailEntity;
import com.JZ.QM.common.entity.WealthAccountEntity;
import com.JZ.QM.common.utils.PageUtils;
import com.JZ.QM.common.utils.Query;
import com.JZ.QM.service.WealthAccountService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/7/11 23:03
 */
@Service()
public class WealthAccountServiceImpl extends ServiceImpl<WealthAccountDao, WealthAccountEntity> implements WealthAccountService {

    @Resource
    PositionDetailDao positionDetailDao;
    @Resource
    ComEarningRecordDao comEarningRecordDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws Exception {
        IPage<WealthAccountEntity> page = this.page(
                new Query<WealthAccountEntity>().getPage(params),
                new QueryWrapper<WealthAccountEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 通过用户id用户持仓信息列表
     * @param accountId 平台账号id
     * @return
     */
    @Override
    public ArrayList<PositionDetailEntity> getPositionDetailList(Long accountId,Long wealthAccountId) {
        //组合收益条件构造器
        QueryWrapper queryWrapperCom = new QueryWrapper();
        //用户的持仓列表
        ArrayList<PositionDetailEntity> positionDetailEntities=new ArrayList<>();
        ComEarningRecordEntity comEarningRecordEntity=new ComEarningRecordEntity();
        //用户持仓信息
        PositionDetailEntity positionDetailEntity=new PositionDetailEntity();
        //定义时间的格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");//设置日期格式
        //定义日历时间
        Calendar c = Calendar.getInstance();
        //前一天时间
        Date start=new Date();
        //前一天日期格式转换
        String combinationUpdateDate="";
        //委托金额
        BigDecimal entrustAmount=new BigDecimal(0);
        //累计收益金额
        BigDecimal totalEarning=new BigDecimal(0);

        //取得前一天日期
        c.add(Calendar.DATE, -1);
        start = c.getTime();
        combinationUpdateDate = dateFormat.format(start);
        //返回还用户所有的持仓信息列表
        positionDetailEntities=positionDetailDao.getPositionDetailList(accountId,wealthAccountId);
        for (Iterator<PositionDetailEntity> it = positionDetailEntities.iterator(); it.hasNext(); ) {
            positionDetailEntity= it.next();
            queryWrapperCom.select("entrust_amount")
                    .eq("wealth_account_id",positionDetailEntity.getAccountId());
            queryWrapperCom.eq("trade_date",combinationUpdateDate);
            //计算持仓的委托金额
            comEarningRecordEntity=comEarningRecordDao.selectOne(queryWrapperCom);
            if(comEarningRecordEntity == null){
                positionDetailEntity.setYesterdayEarning(new BigDecimal(0));
                positionDetailEntity.setTotalEarning(new BigDecimal(0));
            } else {
                entrustAmount=comEarningRecordEntity.getEntrustAmount();
                //计算累积收益
                totalEarning=comEarningRecordDao.getComTotalEarning(positionDetailEntity.getAccountId());
                if(entrustAmount==null){
                    entrustAmount=new BigDecimal(0);
                    totalEarning=new BigDecimal(0);
                }
                positionDetailEntity.setYesterdayEarning(entrustAmount);
                positionDetailEntity.setTotalEarning(totalEarning);
            }
            queryWrapperCom.clear();
        }
        return positionDetailEntities;
    }

    /**
     * 获取用户的总收益
     * @param userId 用户id
     * @return
     */
    @Override
    public PositionDetailEntity getTotalPositionDetail(Long userId,Long accountId) {
        //条件构造器
        QueryWrapper queryWrapper=new QueryWrapper();
        //持仓基本信息
        PositionDetailEntity positionDetailEntity= new PositionDetailEntity();
        //定义时间的格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");//设置日期格式
        //定义日历时间
        Calendar c = Calendar.getInstance();
        //前一天时间
        Date start=new Date();
        //前一天日期格式转换
        String combinationUpdateDate="";
        //昨日累计收益
        BigDecimal yesterdayTotalEarning=new BigDecimal(0);
        //累积持有金额
        BigDecimal totalEarningMoney=new BigDecimal(0);
        //累积持仓收益
        BigDecimal positionTotalEarning=new BigDecimal(0);

        //取得前一天日期
        c.add(Calendar.DATE, -1);
        start = c.getTime();
        combinationUpdateDate = dateFormat.format(start);
        //计算用户持仓的总金额
        totalEarningMoney=positionDetailDao.getTotalEarningMoney(accountId);
        //计算用户持仓的总收益
        positionTotalEarning=positionDetailDao.getPositionTotalEarning(userId,null);
        //计算用户持仓的总收益率
        yesterdayTotalEarning=positionDetailDao.getPositionTotalEarning(userId,combinationUpdateDate);
        //将对应的数值为null的字段直射默认值0;
        if(yesterdayTotalEarning==null){
            yesterdayTotalEarning=new BigDecimal(0);
        }
        if (positionTotalEarning == null){
            positionTotalEarning=new BigDecimal(0);
        }
        if(totalEarningMoney == null){
            totalEarningMoney=new BigDecimal(0);
        }
        //将要返回到额数据进行封装
        positionDetailEntity.setTotalMoney(totalEarningMoney);
        positionDetailEntity.setTotalEarning(positionTotalEarning);
        positionDetailEntity.setYesterdayEarning(yesterdayTotalEarning);
        System.out.println("持仓累计信息为"+positionDetailEntity);
        return positionDetailEntity;


    }
}
